// pages/user/index/index.js
import api from '../../../api/index'
import store from '../../../store/index'
import create from '../../../utils/create'
const app = getApp()
import IMService from '../../../static/lib/imservice.js';


create(store,{

  /**
   * 页面的初始数据
   */
  use:['user'],
  data: {
    login: false,//还没登录
    bTeamNum: 0,//用户建立的团队数量
    aTeamNum: 0,//用户加入的团队数量
    collectNum: 0,//用户收藏的比赛数量
    aTeam:[],//创建队伍列表
    bTeam:[],//加入队伍列表
    wTeam:[]//等待招募的信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
    this.checklogin()

    this.setData({
      collectNum:store.data.user.collect.length
    })
  },
  onShow() {
    this.team()
    this.setData({
      collectNum:store.data.user.collect.length
    })
    var currentUser ={
      uuid: app.globalData.openid,
      name:store.data.user.nickName,//nickName
      avatar : store.data.user.avatarUrl
    }
    app.globalData.imService= new IMService(wx.im);
    app.globalData.imService.connectIM(currentUser);
    if(wx.im.getStatus() === 'disconnected') {
			app.globalData.imService= new IMService(wx.im);
			app.globalData.imService.connectIM(currentUser);
    }

    let self = this;
		wx.im.on(wx.GoEasyIM.EVENT.CONVERSATIONS_UPDATED, (conversations) => {
			// 设置tabBar未读消息总数以及conversation
			self.setConversations(conversations);
		});
	//加载会话列表
  wx.im.latestConversations()
  .then(res => {
    let content = res.content;
    self.setConversations(content);
   this.setData({
    showLoading:false,
    listShow:true
   })
  })
  .catch(e => {
    console.log(e);
    wx.hideLoading();
  });
  },
  setConversations (conversations) {
		conversations.conversations && conversations.conversations.map((item) => {
			// 格式化时间格式
			item.lastMessage.date = app.formatDate(item.lastMessage.timestamp)
		});
		this.setData({
			conversations : conversations.conversations
		},()=>{
				// console.log(this.data.conversations)
		}

		);
		this.setUnreadAmount(conversations.unreadTotal);
  },
	setUnreadAmount(unreadTotal) {
		if(unreadTotal >0){
			wx.setTabBarBadge({
				index: 2,
				text: unreadTotal.toString()
			});
		}else{
			wx.hideTabBarRedDot({
				index :0
			});
		}
	},
  getInfo:function(){
    var openid  = app.globalData.openid
    var params = {openid}
    api.userInfo.getInfo(params).then(res=>{
      // console.log(res.result)
    })
  },
  collect:function(){
    var id = "b00064a7606a4b690d42909061703a6c"
    var params = {id}
    api.userInfo.collect(params).then(res=>{
      // console.log(res)
    })
  },
  uncollect:function(){
    var id = "cbddf0af60681a1300711dc06cb03c97"
    var params = {id}
    api.userInfo.uncollect(params).then(res=>{
      console.log(res)
    })
  },
  collectList:function(){
    api.userInfo.collectList().then(res=>{
      console.log(res)
    })
  },
  tapUserInfo:function() {
    wx.navigateTo({
      url: '../userInfo/index/index',
    })
  },
  toResume:function(){
    wx.navigateTo({
      url: '../Resume/Resume',
    })
  },
  toWorks:function(){
    wx.navigateTo({
      url: '../Works/Works',
    })
  },
  tapMessage:function() {
    console.log("21321")
  },
  tapTo:function(e) {
    var to = e.currentTarget.dataset.to
    console.log(to)
    if(to == '我的收藏')
   {wx.navigateTo({
      url: '/pages/user/interest/interest',
    })}
    else if(to == '创建的队伍'){
      wx.navigateTo({
        url: '/pages/user/myBuildTeam/myBuildTeam?aTeam='+JSON.stringify(this.data.bTeam),
      })
    }
    else if(to == '等待招募'){
      wx.navigateTo({
        url: '/pages/user/waitFor/waitFor',
      })
    }
    else{
      wx.navigateTo({
        url: '/pages/user/myAddTeam/myAddTeam?aTeam='+JSON.stringify(this.data.aTeam),
      })
    }
  },
  tapAbouUs:function(){
    console.log("asdasd")
    wx.navigateTo({
      url: '/pages/aboutus/aboutus',
    })
  },
  // 点击登录函数
  login: function() {
    this.setData({
      login: true
    })
  },
  checklogin:function(){
      if(app.globalData.login){
        this.login()
      }
  },
  team:function(){
    this.aTeam()
    this.bTeam()
    this.wTeam()
  },
  aTeam:function(){
    var status = 'all'
    var type = 'join'
    var params = {status,type}
    const that = this

    api.team.list(params).then(res=>{
      console.log(res.result)
      that.setData({
        aTeam:res.result.data.list
      })
    })
  },
  bTeam:function(){
    var status = 'all'
    var type = 'create'
    var params = {status,type}
    const that = this

    api.team.list(params).then(res=>{
      console.log(res.result)
      that.setData({
        bTeam:res.result.data.list
      })
    })
  },
  wTeam:function(){
    var status = 'all'
    var type = 'create'
    var params = {status,type}
    const that = this

    api.player.list(params).then(res=>{
      console.log(res.result)
      that.setData({
        wTeam:res.result.data.list
      })
    })
  },


  // 分享给朋友
  onShareAppMessage() {
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '玩转组队'
        })
      }, 2000)
    })
    return {
      title: '玩转组队',
      path: '/pages/contest/index/index',
      promise 
    }
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title:'玩转组队',
      imageUrl:'../../../images/wzhn.jpg'
    }
  }
  
})